package org.jivesoftware.smack;

import com.kenai.jbosh.AbstractBody;
import com.kenai.jbosh.BOSHClientResponseListener;
import com.kenai.jbosh.BOSHMessageEvent;
import com.kenai.jbosh.BodyQName;
import com.kenai.jbosh.ComposableBody;
import com.kenai.jbosh.ComposableBody.Builder;
import java.io.StringReader;
import org.jivesoftware.smack.sasl.SASLMechanism.Challenge;
import org.jivesoftware.smack.sasl.SASLMechanism.Failure;
import org.jivesoftware.smack.sasl.SASLMechanism.Success;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

public class BOSHPacketReader
  implements BOSHClientResponseListener
{
  private BOSHConnection connection;

  public BOSHPacketReader(BOSHConnection paramBOSHConnection)
  {
    this.connection = paramBOSHConnection;
  }

  private void parseFeatures(XmlPullParser paramXmlPullParser)
    throws Exception
  {
    int i = 0;
    while (true)
    {
      int j;
      label132: 
      do
        while (true)
        {
          do
            while (true)
            {
              while (true)
              {
                while (true)
                {
                  if (i != 0)
                    return;
                  j = paramXmlPullParser.next();
                  if (j != 2)
                    break label132;
                  if (!(paramXmlPullParser.getName().equals("mechanisms")))
                    break;
                  this.connection.getSASLAuthentication().setAvailableSASLMethods(PacketParserUtils.parseMechanisms(paramXmlPullParser));
                }
                if (!(paramXmlPullParser.getName().equals("bind")))
                  break;
                this.connection.getSASLAuthentication().bindingRequired();
              }
              if (!(paramXmlPullParser.getName().equals("session")))
                break;
              this.connection.getSASLAuthentication().sessionsSupported();
            }
          while (!(paramXmlPullParser.getName().equals("register")));
          this.connection.getAccountManager().setSupportsAccountCreation(true);
        }
      while ((j != 3) || (!(paramXmlPullParser.getName().equals("features"))));
      i = 1;
    }
  }

  public void responseReceived(BOSHMessageEvent paramBOSHMessageEvent)
  {
    AbstractBody localAbstractBody = paramBOSHMessageEvent.getBody();
    if (localAbstractBody != null)
    {
      int i;
      do
      {
        XmlPullParser localXmlPullParser;
        try
        {
          if (this.connection.sessionID == null)
            this.connection.sessionID = localAbstractBody.getAttribute(BodyQName.create("http://jabber.org/protocol/httpbind", "sid"));
          if (this.connection.authID == null)
            this.connection.authID = localAbstractBody.getAttribute(BodyQName.create("http://jabber.org/protocol/httpbind", "authid"));
          localXmlPullParser = XmlPullParserFactory.newInstance().newPullParser();
          localXmlPullParser.setFeature("http://xmlpull.org/v1/doc/features.html#process-namespaces", true);
          localXmlPullParser.setInput(new StringReader(localAbstractBody.toXML()));
          localXmlPullParser.getEventType();
          i = localXmlPullParser.next();
          if ((i == 2) && (!(localXmlPullParser.getName().equals("body"))))
            if (localXmlPullParser.getName().equals("message"))
              this.connection.processPacket(PacketParserUtils.parseMessage(localXmlPullParser));
            else if (localXmlPullParser.getName().equals("iq"))
              this.connection.processPacket(PacketParserUtils.parseIQ(localXmlPullParser, this.connection));
        }
        catch (Exception localException)
        {
          if (!(this.connection.isConnected()))
            return;
          this.connection.notifyConnectionError(localException);
          return;
          if (localXmlPullParser.getName().equals("presence"))
          {
            this.connection.processPacket(PacketParserUtils.parsePresence(localXmlPullParser));
          }
          else if (localXmlPullParser.getName().equals("challenge"))
          {
            String str = localXmlPullParser.nextText();
            this.connection.getSASLAuthentication().challengeReceived(str);
            this.connection.processPacket(new SASLMechanism.Challenge(str));
          }
          else if (localXmlPullParser.getName().equals("success"))
          {
            this.connection.send(ComposableBody.builder().setNamespaceDefinition("xmpp", "urn:xmpp:xbosh").setAttribute(BodyQName.createWithPrefix("urn:xmpp:xbosh", "restart", "xmpp"), "true").setAttribute(BodyQName.create("http://jabber.org/protocol/httpbind", "to"), this.connection.getServiceName()).build());
            this.connection.getSASLAuthentication().authenticated();
            this.connection.processPacket(new SASLMechanism.Success(localXmlPullParser.nextText()));
          }
          else if (localXmlPullParser.getName().equals("features"))
          {
            parseFeatures(localXmlPullParser);
          }
          else if (localXmlPullParser.getName().equals("failure"))
          {
            if ("urn:ietf:params:xml:ns:xmpp-sasl".equals(localXmlPullParser.getNamespace(null)))
            {
              SASLMechanism.Failure localFailure = PacketParserUtils.parseSASLFailure(localXmlPullParser);
              this.connection.getSASLAuthentication().authenticationFailed();
              this.connection.processPacket(localFailure);
            }
          }
          else if (localXmlPullParser.getName().equals("error"))
          {
            throw new XMPPException(PacketParserUtils.parseStreamError(localXmlPullParser));
          }
        }
      }
      while (i != 1);
    }
  }
}